package com.avfh.netcard.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.avfh.netcard.model.Package;
import com.avfh.netcard.service.IPackageService;
import com.avfh.netcard.service.MobileIotService;
import com.avfh.netcard.util.OkHttpUtils;
import com.avfh.netcard.vo.CommonVo;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.avfh.netcard.common.BaseController;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 充值套餐表 前端控制器
 * </p>
 *
 * @author wuhui
 * @since 2017-06-26
 */
@RestController
@RequestMapping("/api/package")
public class PackageController extends BaseController {
    @Autowired
    IPackageService packageService;


    /**
     * 流量套餐集合
     * @param request
     */
    @PostMapping("/list")
    public void list(HttpServletRequest request, HttpServletResponse response) {
        List<Package> list = packageService.selectList(null);
        renderJson(response, list);
    }


    /**
     * 本月使用流量
     * @param sim      卡号
     */
    @PostMapping("/usedByCurMonth")
    public void usedByCurMonth(HttpServletRequest request, HttpServletResponse response, String sim) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String startTime = sdf.format(new Date());
        String endTime = sdf.format(new Date());
        JSONObject jsonObject = MobileIotService.memberMonthPackageQuery(sim, startTime, endTime);
        CommonVo commonVo = JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference<CommonVo>() {
        });
        if (!StringUtils.isEmpty(commonVo.getResultCode()) && commonVo.getResultCode().equals("0")) {
            renderJson(response, commonVo.getOlMberMmFlows().get(0).getMmflowNum());
            return;
        }
        renderJson(response, commonVo.getResultMsg());
    }
}
